import { message } from 'antd';
import {
  getBillState,
  getReceive,
  getList,
  getPayBillDetail,
  getDetailList,
  refuseStatus,
  confirmStatus,
  markPay,
  singleExportFile,
  mergeExportFile,
  getUploadFile,
} from '@/services/paybill';

export default {
  state: {
    formFieldsValue: {},
    expandForm: false,
    data: {
      // 收费首页列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    tabList: [{ itemCode: 1, itemName: '账单信息' }, { itemCode: 2, itemName: '账单明细' }],
    billStateList: [], // 账单状态字典
    receiveList: [], // 接单方列表
    selectedKeys: [],
    selectedRows: [],
    payBillDetail: {},
    detailList: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    valuesArr: [
      'recvSuppId',
      'billYm',
      'billState',
      'billNo',
    ]
  },
  effects: {
    // 查询列表
    *fetchList({ payload }, { call, put }) {
      const res = yield call(getList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (res.success) {
        yield put({
          type: 'getList',
          payload: {
            data: res.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(res.message);
      }
    },
    // 标记以支付
    *markPay({ payload }, { call }) {
      const res = yield call(markPay, payload);
      return res;
    },
    // 查询账单状态
    *fetchBillState({ payload }, { call, put }) {
      const res = yield call(getBillState, payload);
      if (res.success) {
        yield put({
          type: 'getBillState',
          payload: res.data,
        });
      } else {
        message.warning(res.message);
      }
    },

    // 查询接单方
    *fetchReceive({ payload }, { call, put }) {
      const res = yield call(getReceive, payload);
      if (res.success) {
        yield put({
          type: 'getReceive',
          payload: res.data,
        });
      } else {
        message.warning(res.message);
      }
    },
    // 查询详情
    *fetchDetail({ payload }, { call, put }) {
      const res = yield call(getPayBillDetail, payload);
      if (res.success) {
        yield put({
          type: 'getDetail',
          payload: res.data,
        });
      } else {
        message.warning(res.message);
      }
    },
    // 详情列表
    *fetchDetailList({ payload }, { call, put }) {
      const res = yield call(getDetailList, payload);
      const { currentPage, pageSize } = payload.pageArg;
      if (res.success) {
        yield put({
          type: 'getDetailList',
          payload: {
            data: res.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(res.message);
      }
      return res;
    },
    // 拒绝
    *refuseStatus({ payload, callback }, { call }) {
      const res = yield call(refuseStatus, payload);
      if (callback) {
        callback(res);
      }
    },
    // 确认
    *confirmStatus({ payload, callback }, { call }) {
      const res = yield call(confirmStatus, payload);
      if (callback) {
        callback(res);
      }
    },
    // 账单单个下载
    *singleExport({ payload }, { call }) {
      const response = yield call(singleExportFile, payload);
      return response;
    },
    // 账单合并下载
    *mergeExport({ payload }, { call }) {
      const response = yield call(mergeExportFile, payload);
      return response;
    },
    // 全国 付费账单 - 账单下载
    *uploadFile({ payload }, { call }) {
      const response = yield call(getUploadFile, payload);
      return response;
    },
  },
  reducers: {
    getBillState(state, { payload }) {
      return {
        ...state,
        billStateList: Array.isArray(payload.NSBILL_PAY_STATE) ? payload.NSBILL_PAY_STATE : [],
      };
    },
    getReceive(state, { payload }) {
      return {
        ...state,
        receiveList: Array.isArray(payload) ? payload : [],
      };
    },
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: { list, pagination: { total: data.totalCount, current: currentPage, pageSize } },
      };
    },
    selectRow(
      state,
      {
        payload: { selectedRows, selectedKeys },
      }
    ) {
      return {
        ...state,
        selectedKeys,
        selectedRows,
      };
    },
    getDetail(state, { payload }) {
      return {
        ...state,
        payBillDetail: payload,
      };
    },
    getDetailList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        detailList: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
